package start.doubudouyin.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import start.doubudouyin.entity.Usersinfo;
import start.doubudouyin.service.AdminregisterService;
import start.doubudouyin.service.FollowService;
import start.doubudouyin.service.UsersinfoService;
import start.doubudouyin.utils.Result;

import java.util.List;

@RestController
@RequestMapping("/follow")
@CrossOrigin(origins = "http://localhost:5173/")
public class FollowController {

    @Resource
    UsersinfoService usersinfoService;
    @Resource
    FollowService followService;

    //用户关注
    @PutMapping("/userfollow")
    public Result userLike(@RequestParam("followerId") String followerId, @RequestParam("followedId") String followedId) {
        Usersinfo usersinfo = usersinfoService.getUserById(followedId);
        if (usersinfo == null) {
            return Result.error("300", "用户不存在");
        }
        if (followService.isFollow(followerId, followedId) != null) {
            return Result.error("300", "已经关注，请勿重复添加");
        }
        if (followService.insert(followerId, followedId)) {
            return Result.success();
        } else {
            return Result.error("300", "关注失败");
        }
    }

    //获取用户关注数
    @GetMapping("/getfollowedCount")
    public Result getfollowedCount(@RequestParam("userId") String userId) {
        Integer count = followService.GetFollowedCount(userId);
        if (count == null) {
            return Result.error("300", "系统错误，请稍后再试!");
        }
        return Result.success(count);
    }

    //获取用户粉丝数
    @GetMapping("/getfansCount")
    public Result getfansCount(@RequestParam("userId") String userId) {
        Integer count = followService.GetFansCount(userId);
        if (count == null) {
            return Result.error("300", "系统错误，请稍后再试!");
        }
        return Result.success(count);
    }

    @GetMapping("/getfirendId")
    public Result getfirendId(@RequestParam("followerId") String followerId) {
        List<String> list = followService.getfirendId(followerId);
        if (list == null) {
            return Result.error("300", "获取朋友列表失败");
        }
        return Result.success(list);
    }

    @GetMapping("/getfollowedId")
    public Result getfollowedId(@RequestParam("followerId") String followerId) {
        List<String> list = followService.getfollowedId(followerId);
        if (list == null) {
            return Result.error("300", "获取关注列表失败");
        }
        return Result.success(list);
    }

    //获取关注的用户信息
    @GetMapping("/getFollowedInfo")
    public Result getFollowedInfo(@RequestParam("userId") String userId) {
        List<Usersinfo> list = usersinfoService.getFollowedInfo(userId);
//        if (list == null) {
//            return Result.error("300", "获取用户信息失败");
//        }
        return Result.success(list);
    }
}
